/** 创建组件对象 * */
var benchmarkForm = new Form();
var benchmarkTree = new Tree();
var benchmarkTable = new Table();
var benchmarkPage = new Page();
var benchmarkDialog = new Dialog();
var benchmarkAjax = new Ajax();
var iframeIndex;
var benchmarkObj =[];
var keyName = "indicator";
var objName = "benchmarkObj";

/** 页面加载完执行 * */
$(function() {
	initScript();
	initBenchmarkTree();
	initButton();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/benchmark/locale/Benchmark_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/benchmark/properties/BenchmarkDesignValueProperties_Dialog.js"); // 2.加载配置js(非必配,配置也可写在本js中)
	loadScript(appPath + "/project-yddemo/common/Common.js");
}

function initBenchmarkTree() {
	if(window.parent[objName]!=undefined){
		benchmarkObj = window.parent[objName];
	}
	var indicatorIds = [];
	var data = {};
	for(var i = 0;i<benchmarkObj.length;i++){
		indicatorIds.push(benchmarkObj[i].id);
	}
	if(indicatorIds.length!=0){
		data.indicatorIds = indicatorIds.join(",");
	}else{
		data.indicatorIds = "";
	}
	benchmarkTree.initTree({
		divId : "benchmarkTreeDiv", // 归属div*
		ajaxType : "ajax",
		root : {
			text : benchmarkLocale.text.benchmarkRoot,
			leaf : false,
			expanded : true
		},
		dataAjax : { // 数据源ajax*
			url : requestPath + "/yddemo/benchmarks/dialog", // 请求url*
			async : false, // 是否异步*
			type : "GET", // 请求类型*
			data : data
		},
		align : { // 元素顺序
			left : "expand",
			right : "check",
			center : "text"
		},
		checkIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_check.png", // 选中图标
		partcheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_partcheck.png", // 部分选图标
		uncheckIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_tree_uncheck.png", // 未选图标
		expandIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_expand.png", // 展开图标
		closeIcon : staticPath + "/skin/" + skinType
				+ "/icon/Button_main_shrink.png", // 闭合图标
		textClickFun : function(nodeId) { // 文本单击事件*
			benchmarkTree.expandDefaultFun(nodeId);
		},
		expandFun : function(nodeId) { // 展开单击事件*
			benchmarkTree.expandDefaultFun(nodeId);
		},
		checkFun : function(nodeId){
//			benchmarkTree.expandDefaultFun(nodeId);
			benchmarkTree.checkDefaultFun(nodeId);
		}/*,
		models:BenchmarkDesignValueProperties_Dialog.getTreeModels(benchmarkObj),*/
	});
	if (!$.isEmptyObject(sessionStorage.keyName)) {
		keyName = sessionStorage.keyName;
		sessionStorage.removeItem("keyName");
	}
	if (!$.isEmptyObject(sessionStorage.objName)) {
		objName = sessionStorage.objName;
		sessionStorage.removeItem("objName");
	}
}

function initButton() {
	$(".formButtonDiv")
			.append(
					"<div style='width:50%;'>"
							+ "<span class='formButton' style='float:right; margin-right:10%;' onclick='closeDialog()'>"
							+ benchmarkLocale.text.close
							+ "</span>"
							+ "</div>"
							+ "<div class='action' style='width:50%;'>"
							+ "<span class='formButton' style='float:left; margin-right:10%;' onclick='saveData()'>"
							+ benchmarkLocale.text.okey + "</span>" + "</div>");
}

/** 保存数据 * */
function saveData() {
	benchmarkObj = benchmarkTree.getCheckedNodes();
	window.parent[objName] = benchmarkObj;
	setBenchmarkInput();
	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function closeDialog() {
	// 关闭弹出框
	var div = window.frameElement.parentElement;
	var shadeDiv = $(div).parent();
	$(div).remove();
	if (shadeDiv.children().length == 0) {
		shadeDiv.remove();
	}
}

function setBenchmarkInput() {
	var indicators = [];
	var packages = [];
	for(var i = 0;i<benchmarkObj.length;i++){
		if(benchmarkObj[i].entityType != "IndicatorDetail"){
			packages.push(benchmarkObj[i].id);
			continue;
		}else{
			indicators.push(benchmarkObj[i].id);
		}
	}
	benchmarkObj = getIndicatorDetail(indicators);
	if (benchmarkObj != undefined) {
		var valContext="";
		for(var i = 0;i<benchmarkObj.length;i++){
			valContext += benchmarkObj[i].indicator.name+",";
		}
		window.parent.$("[key$='" + keyName + "']").val(valContext);
	}
}